AC_REVISION($Id: $)dnl

AC_PREREQ(2.56)
AC_INIT(DoorOpener, 1.0.0, [info@tt-solutions.com])
AM_INIT_AUTOMAKE()

AC_CONFIG_SRCDIR([makefile.am])
AC_CONFIG_HEADER([include/objectdet/config.h])

AC_CANONICAL_HOST

USE_LINUX=0
USE_WINDOWS=0
case "${host}" in
    *-*-linux* )
        USE_LINUX=1;
        ;;

    *-*-cygwin* )
        USE_CYGWIN=1;
        USE_WINDOWS=1;
        ;;

    *-*-mingw32* )
        USE_MINGW32=1;
        USE_WINDOWS=1;
        ;;
esac

AM_CONDITIONAL(OBJECTDET_MSW, [test "$USE_WINDOWS" = 1])

AC_ARG_WITH(boost-headers,
            [AC_HELP_STRING([--with-boost-headers=dir], [Boost headers directory])],
            objectdet_boost_headers=$withval)
AC_ARG_WITH(boost-libs,
            [AC_HELP_STRING([--with-boost-libs=dir], [Boost libs installation directory])],
            objectdet_boost_libs=$withval)
AC_ARG_WITH(boost-toolkit,
            [AC_HELP_STRING([--with-boost-toolkit=dir], [Boost installation toolkit (gcc, mgw, etc.)])],
            objectdet_boost_toolkit=$withval)

AC_ARG_WITH(opencv-headers,
            [AC_HELP_STRING([--with-opencv-headers=dir], [Opencv headers directory])],
            objectdet_opencv_headers=$withval)
AC_ARG_WITH(opencv-libs,
            [AC_HELP_STRING([--with-opencv-libs=dir], [Opencv libs installation directory])],
            objectdet_opencv_libs=$withval)

CFLAGS=${CFLAGS:=}
AC_PROG_CC

CXXFLAGS=${CXXFLAGS:=}
AC_PROG_CXX

AC_PROG_LD

AC_LANG(C++)

AC_DISABLE_STATIC   dnl don't waste time on static build by default
AC_PROG_LIBTOOL


dnl --- Boost (required) ---
if test "x$objectdet_boost_headers" != "x"; then
    CPPFLAGS="$CPPFLAGS -I$objectdet_boost_headers"
    errmsg="even in $objectdet_boost_headers"
else
    errmsg="use --with-boost-headers=dir
if they are installed in non default location"
fi

AC_CHECK_HEADER([boost/type_traits.hpp],
    [],
    [AC_MSG_ERROR([Boost headers not found, $errmsg])]
)

if test "x$objectdet_boost_toolkit" == "x"; then
    if test "x$USE_MINGW32" = "x1"; then
        objectdet_boost_toolkit=mgw
    else
        objectdet_boost_toolkit=gcc
    fi
fi

boost_libfs="boost_filesystem-$objectdet_boost_toolkit"

save_LDFLAGS=$LDFLAGS
if test "x$objectdet_boost_libs" != "x"; then
    BOOST_LDFLAGS="-L$objectdet_boost_libs "
    LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
    errmsg="even in $objectdet_boost_libs"
else
    errmsg="use --with-boost-libs=dir
if it is installed in non default location"
fi

LIBS="$LIBS -l$boost_libfs"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <boost/filesystem/operations.hpp>]],
    [[boost::filesystem::current_path()]])],
    [],
    [AC_MSG_ERROR([Boost filesystem library $boost_libfs not found, $errmsg])]
)

LDFLAGS=$save_LDFLAGS

dnl --- opencv (required) -------------------
if test "x$objectdet_opencv_headers" != "x"; then
    CPPFLAGS="$CPPFLAGS -I$objectdet_opencv_headers"
    errmsg="even in $objectdet_opencv_headers"
else
    errmsg="use --with-opencv-headers=dir
if they are installed in non default location"
fi

AC_CHECK_HEADER([cv.h],
    [],
    [AC_MSG_ERROR([Opencv headers not found, $errmsg])]
)

save_LIBS=$LIBS
save_LDFLAGS=$LDFLAGS
if test "x$objectdet_opencv_libs" != "x"; then
    OPENCV_LDFLAGS="-L$objectdet_opencv_libs "
    errmsg="even in $objectdet_opencv_libs"
else
    OPENCV_LDFLAGS=""
    errmsg="use --with-opencv-libs=dir
if it is installed in non default location"
fi
OPENCV_LIBS="-lcv -lcxcore -lcvaux -lhighgui"

LDFLAGS="$LDFLAGS $OPENCV_LDFLAGS"
LIBS="$LIBS $OPENCV_LDFLAGS $OPENCV_LIBS"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <cv.h>]],
    [[CvMat mat; mat.type = 1]])],
    [],
    [AC_MSG_ERROR([Opencv libraries not found, $errmsg])]
)

LDFLAGS=$save_LDFLAGS
LIBS=$save_LIBS
AC_SUBST(OPENCV_LDFLAGS)
AC_SUBST(OPENCV_LIBS)

dnl --- libxml++ (required) -------------------
PKG_CHECK_MODULES(LIBXMLPP, libxml++-2.6 >= 2.6.0)

dnl === Generate output files ===
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT
